package controle;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class BackupBD {


    public void cria_bkpbd(String caminho_out) {

        String inFile = "./db/tarefas.script";
        String outFile = caminho_out;

        InputStream is = null;
        OutputStream os = null;
        byte[] buffer;
               try {

            is = new FileInputStream(inFile);
            os = new FileOutputStream(outFile);
            buffer = new byte[is.available()];
            is.read(buffer);
            os.write(buffer);

        } catch (IOException e) {
            System.out.println(e);
        
        } catch (OutOfMemoryError e) {
            System.out.println(e);

        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {

                System.out.println(e);
            }
        }
    }


    public void cria_bkpbd_ao_sair() {

        String inFile = "./lib/bd/financas.script";
        String outFile = "./lib/bkp/financas.script";

        InputStream is = null;
        OutputStream os = null;
        byte[] buffer;
               try {

            is = new FileInputStream(inFile);
            os = new FileOutputStream(outFile);
            buffer = new byte[is.available()];
            is.read(buffer);
            os.write(buffer);

        } catch (IOException e) {
            System.out.println(e);
        
        } catch (OutOfMemoryError e) {
            System.out.println(e);

        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {

                System.out.println(e);
            }
        }
    }



    public void restaura_bkpbd(String caminho_in) {

        String inFile = caminho_in;
        String outFile = "./lib/bd/financas.script";

        InputStream is = null;
        OutputStream os = null;
        byte[] buffer;
               try {

            is = new FileInputStream(inFile);
            os = new FileOutputStream(outFile);
            buffer = new byte[is.available()];
            is.read(buffer);
            os.write(buffer);

        } catch (IOException e) {
            System.out.println(e);

        } catch (OutOfMemoryError e) {
            System.out.println(e);

        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {

                System.out.println(e);
            }
        }
    }
}
